var baseUrl = "";

var timer = null;

var vue = new Vue({
    el: '#app',
    data() {
        return {
            tempForm: {
                mobile: '',
                code: '',
            },
            step: 0,
            timeVisible: false,
            num: 60,
            btnLoading: false,
            submitLoading: false,
            radio:false,
            mobileTwo: '',
        }
    },
    mounted() {

    },
    watch: {
        // 监听mobile，如果mobile的长度等于11，step变为1,如过长度大于11，去掉多余的字符
        'tempForm.mobile': function (val) {
            if (val.length === 11) {
                this.step = 1;
            } else if (val.length > 11) {
                this.tempForm.mobile = val.slice(0, 11);
            }
        },
        'tempForm.code': function (val) {
            if (val.length === 6) {
                if (this.step === 2) {
                    this.step = 3;
                }
            } else if (val.length > 6) {
                this.tempForm.code = val.slice(0, 6);
            }
        }
    },
    methods: {

        handlePhone() {
          window.location.href = "tel:4000022056";
        },

        //产品/优惠预受理接口(办理资格校验)
        check(){
            if (this.tempForm.mobile.length !== 11) {
                vant.Toast("手机号码不正确");
                this.step = 0;
                return;
            }
            if (this.btnLoading) {
                return;
            }
            this.btnLoading = true;
            this.timeVisible = true;
            if (timer){
                clearInterval(timer);
            }
            timer = setInterval(() => {
                this.num--;
                if (this.num === 0) {
                    clearInterval(timer);
                    this.timeVisible = false;
                    this.num = 60;
                    this.btnLoading = false;
                }
            }, 1000);
            console.log(this.radio)
            if (this.radio) {
                this.getCode()
            }else {
                vant.Toast("请确认您已阅读并同意《业务受理协议》");
                this.btnLoading = false;
                this.timeVisible = false;
                clearInterval(timer);
                this.num = 60;
            }

        },

        getCode() {
            const settings = {
                url: baseUrl + '/CrbtByProvice/hunan/sendSmsVerifyCodeOut',
                method: 'post',
                headers: {
                    "Content-Type": "application/json"
                },

                data: JSON.stringify({
                    discntCode: "99785755",
                    fee: "2000",
                    cpid: "test",
                    mobile: this.tempForm.mobile,
                    tradeTypeCode: "110",
                    appId: "710313"
                })
            }

            let _this = this;

            $.ajax(settings).done((res) => {
                console.log(res);

                if (res.state === "success") {
                    const data = res.data;
                    if (data.respCode === '0') {
                        vant.Toast("发送成功");

                    } else {
                        if (timer){
                            clearInterval(timer);
                        }

                        this.timeVisible = false;
                        this.num = 60;
                        this.btnLoading = false;
                        alert(data.respDesc);
                    }
                }
            }).fail((error) => {
                console.log(error);
                clearInterval(timer);
                _this.timeVisible = false;
                _this.num = 60;
                _this.btnLoading = false;
                vant.Toast(error.statusText);
            })


        },
        submit() {
            if (this.tempForm.mobile.length!==11||this.tempForm.code.length !== 6) {
                vant.Toast("请填写正确的手机号码和验证码");
                return;
            }
            if (this.btnLoading || this.submitLoading) {
                return;
            }

            this.step = 3;
            // mobileTwo是mobile的脱敏后的值，用于展示，显示前三位和后四位
            this.mobileTwo = this.tempForm.mobile.slice(0, 3) + "XXXX" + this.tempForm.mobile.slice(7, 11);

        },

        handleClose() {
            this.step = 1;
            this.tempForm.mobile = '';
            this.tempForm.code = '';
        },

        //产品/优惠受理
        accept(){
            if (this.submitLoading) {
                vant.Toast("请勿重复提交");
            }
            const settings = {
                url: baseUrl + '/CrbtByProvice/hunan/productChangeOut',
                method: 'post',
                headers: {
                    "Content-Type": "application/json"
                },
                data: JSON.stringify({
                    mobile: this.tempForm.mobile,
                    discntCode: "99785755",
                    cpid: "test",
                    fee: "2000",
                    appId: "710313",
                    tradeTypeCode: "110",
                    verifyCode: this.tempForm.code,
					productName:"动感地带电竞包《穿越火线:枪战王者》20元档（2024）",
					keepType:"0",
                })
            }

            $.ajax(settings).done((res) => {
                console.log(res);
                if (res.state === "success") {
                    const data = res.data;
                    if (data.respCode === '0') {
                        console.log("提交成功");
                        console.log("提交成功");
                        this.step = 4;
                    } else {
                        vant.Toast(data.respDesc);
                        this.step = 5;
                    }
                    this.submitLoading = false;
                } else {
                    vant.Toast(res.data.respDesc);
                    this.step = 5;
                    this.submitLoading = false;
                }
            }).fail((error) => {
                console.log(error);
                vant.Toast(error.statusText);
                this.submitLoading = false;
            })
        },
    }
})
